package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 收藏 取消收藏
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {

        if (dto.getArticleId()==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"文章id不能为空");
        }
        //得到登录用户的id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto.getType().intValue()!= 0&&dto.getType().intValue()!= 1 ){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"收藏方式只能是0或者1");
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (dto.getType().intValue()== 0){
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApCollection one = mongoTemplate.findOne(query, ApCollection.class);
            if (one!=null){
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN,"已经收藏过");
            }
            ApCollection apCollection = new ApCollection();
            apCollection.setEntryId(apBehaviorEntry.getId());
            apCollection.setArticleId(dto.getArticleId());
            apCollection.setType((short) 0);
            apCollection.setCollectionTime(apBehaviorEntry.getCreatedTime());
            mongoTemplate.save(apCollection);

        }else {
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApCollection one = mongoTemplate.findOne(query, ApCollection.class);
            if (one == null){
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN,"请先收藏");
            }
            mongoTemplate.remove(query,ApCollection.class);
        }
        return ResponseResult.okResult();
    }
}
